package com.feidian.helper.ui;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.provider.Settings;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;
import android.widget.Toast;

import com.feidian.helper.R;
import com.feidian.helper.bean.Item;
import com.feidian.helper.bean.Table;
import com.feidian.helper.bean.User;
import com.feidian.helper.net.NetManager;
import com.feidian.helper.net.NetworkDetector;
import com.feidian.helper.ui.NewsDetailsActivity.GetHelpObjectThread;
import com.feidian.helper.ui.fragment.AcceptFragment;
import com.feidian.helper.ui.view.MyDialog;

public class NewsDetailsAcceptActivity extends Activity implements
		View.OnClickListener {

	private static final int GET_CONTENT_SUCCESS = 0X123; // 接单成功
	private static final int GET_CONTENT_FAILURE = 0X124; // 接单失败

	private static final int GIVE_UP_OBJECT_SUCCESS = 0X125; //放弃单子成功
	private static final int GIVE_UP_OBJECT_FAILURE = 0X126;

	private ImageView mBackView;
	private TextView mTitle;

	private TextView mNameTv;
	private TextView mStartTimeTv;
	private TextView mTitleTv;
	private TextView mContentTv;
	private TextView mStateTv;
	private TextView mQQTv;
	private TextView mPhoneTv;
	private TextView mPrice;
	private TextView mEndLine;
	private RatingBar ratingBar;
	private TextView mAcceptTv;
	private Button acceptBn;
	
	private Handler mHandler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case GET_CONTENT_SUCCESS: {
				Toast.makeText(NewsDetailsAcceptActivity.this, "接单成功",
						Toast.LENGTH_LONG).show();
				finish();
			}
				break;
			case GET_CONTENT_FAILURE: {

			}
				break;
			case GIVE_UP_OBJECT_SUCCESS: {
				Toast.makeText(NewsDetailsAcceptActivity.this, "成功取消订单",
						Toast.LENGTH_LONG).show();
				getItem().setState("0");
				AcceptFragment.isRefresh=true;
				finish();

			}
				break;
			case GIVE_UP_OBJECT_FAILURE: {
				Toast.makeText(NewsDetailsAcceptActivity.this, "取消订单失败",
						Toast.LENGTH_LONG).show();
				//mDialog.dismiss();
			}
				break;
			default:
				break;
			}
		};
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_news_details_accept);
		initView();
	}

	private void initView() {
		mBackView = (ImageView) findViewById(R.id.back);
		mBackView.setVisibility(View.VISIBLE);
		mBackView.setOnClickListener(this);
		mTitle = (TextView) findViewById(R.id.title);
		mTitle.setText("懒人帖子详情");
		mNameTv = (TextView) findViewById(R.id.name);

		mStartTimeTv = (TextView) findViewById(R.id.past_time);

		mTitleTv = (TextView) findViewById(R.id.my_title);

		mContentTv = (TextView) findViewById(R.id.content);

		mStateTv = (TextView) findViewById(R.id.state);

		mQQTv = (TextView) findViewById(R.id.qq);

		mPhoneTv = (TextView) findViewById(R.id.phone);

		mPrice = (TextView) findViewById(R.id.price);

		mEndLine = (TextView) findViewById(R.id.endline);

		ratingBar = (RatingBar) findViewById(R.id.ratingBar);

		mAcceptTv = (TextView) findViewById(R.id.accept_name);

		acceptBn = (Button) findViewById(R.id.accept_button);
		acceptBn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				showGiveUpObjectDialog();
			}
		});

		upData();

	}

	void upData() {
		if (getItem().getName() != null
				|| !getItem().getName().trim().equals("null"))
			mNameTv.setText(getItem().getName());
		else
			mNameTv.setText("出错");
		mStartTimeTv.setText(getItem().getPastTime());
		mTitleTv.setText(getItem().getTitle());
		mContentTv.setText(getItem().getContent());
		mQQTv.setText(getItem().getQQ().equals("null")?"无":getItem().getQQ());
		mPhoneTv.setText(getItem().getTel().equals("null")?"无":getItem().getTel());
		mPrice.setText(getItem().getPrice().equals("null")?"无":getItem().getPrice());
		mEndLine.setText(getItem().getEndline());
		if (getItem().getAccepName() != null
				|| getItem().getAccepName().equals("null"))
			mAcceptTv.setText(getItem().getAccepName());
		if (getItem().getState().equals("0")) {
			mStateTv.setText("未接单");
		} else if (getItem().getState().equals("1")) {
			mStateTv.setText("处理中");
			acceptBn.setFocusable(false);
			acceptBn.setEnabled(false);
			acceptBn.setText("处理中");
			acceptBn.setBackgroundColor(Color.GRAY);
		} else if (getItem().getState().equals("2")) {
			mStateTv.setText("已经处理");
			mStateTv.setText("已经处理");
			acceptBn.setFocusable(false);
			acceptBn.setEnabled(false);
			acceptBn.setText("已经处理");
			acceptBn.setBackgroundColor(Color.GRAY);
		} else {
			mStateTv.setText("出错");
		}

		if (getItem().getHonesty() != null
				&& !getItem().getHonesty().equals("null")) {
			ratingBar.setRating(Float.valueOf(getItem().getHonesty()));
		}

		else {
			ratingBar.setRating(5);
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.back:
			finish();
			break;
		}
	}

	Item getItem() {
		return AcceptFragment.itemList.get(AcceptFragment.pos);
	}

	class GiveUpObjectThread extends Thread {
		private String mUrl;
		private List<NameValuePair> mParams;

		public GiveUpObjectThread(String url, List<NameValuePair> params) {
			// TODO Auto-generated constructor stub
			mUrl = url;
			mParams = params;
		}

		@Override
		public void run() {
			// TODO Auto-generated method stub
			String context = NetManager.getNetManager().getWebWithPost(mUrl,
					mParams);
			if (context != null&&context.equals("1")) {

				Log.e("contenxt("
						+ NewsDetailsAcceptActivity.this.getClass().toString()
						+ ")=", context);
				Message msg = mHandler.obtainMessage();
				msg.obj = context;
				msg.what = GIVE_UP_OBJECT_SUCCESS;
				mHandler.sendMessage(msg);
			}

			else {
				Log.e("context=", "null");
				Message msg = mHandler.obtainMessage();
				msg.what = GIVE_UP_OBJECT_FAILURE;
				mHandler.sendMessage(msg);
			}

			super.run();
		}
	}

	private void showGiveUpObjectDialog() {
		if(!NetworkDetector.detect(NewsDetailsAcceptActivity.this)){
			View view = getLayoutInflater().inflate(R.layout.dialog,null);
        	final MyDialog dialog = new MyDialog(NewsDetailsAcceptActivity.this,view,R.style.dialog);
        	TextView dialog_title = (TextView)view.findViewById(R.id.dialog_title);
        	dialog_title.setText("当前网络不可用");
        	TextView dialog_content = (TextView)view.findViewById(R.id.dialog_content);
        	dialog_content.setText("是否进行网络设置？");
        	Button dialog_sure = null;
        	Button dialog_cancel = null;
        	dialog_sure = (Button)view.findViewById(R.id.button1);
        	dialog_cancel = (Button)view.findViewById(R.id.button2);
        	dialog_sure.setText("是");
            dialog_cancel.setText("否");
        	dialog_sure.setOnClickListener(new OnClickListener(){
        		@Override
        		public void onClick(View v){
        			startActivity(new Intent(    
                            Settings.ACTION_WIRELESS_SETTINGS));
        		}
        	});
        	dialog_cancel.setOnClickListener(new OnClickListener(){
        		@Override
        		public void onClick(View view){
        			dialog.dismiss();
        		}
        	});
        	dialog.show();
		}else{
			if (User.stuId == null || User.stuId.equals("")
					|| User.password == null || User.password.equals("")) {
				Toast.makeText(NewsDetailsAcceptActivity.this, "用户尚未登录",
						Toast.LENGTH_LONG).show();
				return;
			}
			View view = getLayoutInflater().inflate(R.layout.dialog,null);
        	final MyDialog dialog = new MyDialog(NewsDetailsAcceptActivity.this,view,R.style.dialog);
        	TextView dialog_title = (TextView)view.findViewById(R.id.dialog_title);
        	dialog_title.setText("懒人帮提示");
        	TextView dialog_content = (TextView)view.findViewById(R.id.dialog_content);
        	dialog_content.setText("确认放弃接单？");
        	Button dialog_sure = null;
        	Button dialog_cancel = null;
        	dialog_sure = (Button)view.findViewById(R.id.button1);
        	dialog_cancel = (Button)view.findViewById(R.id.button2);
        	dialog_sure.setText("放弃接单");
            dialog_cancel.setText("取消");
        	dialog_sure.setOnClickListener(new OnClickListener(){
        		@Override
        		public void onClick(View v){
        			if (getItem().getState().equals("1")) {
    					String url = "http://115.29.103.105/User/gvObject";
    					List<NameValuePair> params = new ArrayList<NameValuePair>();
    					Log.e("stuId=",User.stuId+"");
    					Log.e("id=",getItem().getId());
    					params.add(new BasicNameValuePair("myId", User.stuId));
    					params.add(new BasicNameValuePair(Table.id, getItem()
    							.getId()));
    					GiveUpObjectThread getHelpObjectThread = new GiveUpObjectThread(
    							url, params);
    					getHelpObjectThread.start();
    				}
        		}
        	});
        	dialog_cancel.setOnClickListener(new OnClickListener(){
        		@Override
        		public void onClick(View view){
        			dialog.dismiss();
        		}
        	});
        	dialog.show();
		}
        /*
		final Dialog mDialog = new Dialog(NewsDetailsAcceptActivity.this,
				R.style.dialog);
		LayoutInflater layoutInflater = NewsDetailsAcceptActivity.this
				.getLayoutInflater();
		View view = layoutInflater.inflate(R.layout.logout_dialog, null, false);
		TextView content=(TextView)view.findViewById(R.id.content);
		content.setText("是否放弃单子??");
		Button mSure = (Button) view.findViewById(R.id.sure);
		Button mCancel = (Button) view.findViewById(R.id.no);
		mSure.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				
				if (getItem().getState().equals("1")) {
					String url = "http://115.29.103.105/User/gvObject";
					List<NameValuePair> params = new ArrayList<NameValuePair>();
					Log.e("stuId=",User.stuId+"");
					Log.e("id=",getItem().getId());
					params.add(new BasicNameValuePair("myId", User.stuId));
					params.add(new BasicNameValuePair(Table.id, getItem()
							.getId()));
					GiveUpObjectThread getHelpObjectThread = new GiveUpObjectThread(
							url, params);
					getHelpObjectThread.start();
				}
			}
		});
		mCancel.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				mDialog.dismiss();
			}
		});
		mDialog.setContentView(view);
		mDialog.show();
		*/
	}
}
